#include "PriceDataServiceClient.h"


PriceDataServiceClient::PriceDataServiceClient(){
    setServiceID(10004);
    m_socketID = -1;
}

int PriceDataServiceClient::getSocketID() {
    return m_socketID;
}

void PriceDataServiceClient::setSocketID(int value) {
    m_socketID = value;
}

void PriceDataServiceClient::subCodes(int requestID, String codes) {
    FCBinary bw;
    bw.writeString(codes);
    char* bytes = (char*)bw.getBytes();
    FCMessage fcm(getServiceID(), FUNCTION_SUBCODES, requestID, m_socketID, 0, getCompressType(), bw.getBytesLen(), bytes);
    send(&fcm);
}

void PriceDataServiceClient::unSubCodes(int requestID) {
    FCBinary bw;
    bw.writeString("1");
    char* bytes = (char*)bw.getBytes();
    FCMessage fcm(getServiceID(), FUNCTION_UNSUBCODES, requestID, m_socketID, 0, getCompressType(), bw.getBytesLen(), bytes);
    send(&fcm);
}

ArrayList<PriceData> PriceDataServiceClient::getPriceDatas(char* body, int bodyLength) {
    FCBinary br;
    br.write(body, bodyLength);
    ArrayList<PriceData> priceDatas;
    if (body  && bodyLength > 0)
    {
        int datasSize = br.readInt();
        for (int i = 0; i < datasSize; i++)
        {
            PriceData data;
            data.m_code = br.readString();
            data.m_lastClose = br.readFloat();
            data.m_close = br.readFloat();
            int type = br.readChar();
            if (type == 0)
            {
                data.m_volume = br.readDouble();
                data.m_amount = br.readDouble();
            }
            else
            {
                data.m_volume = br.readInt();
                data.m_amount = br.readInt();
            }
            data.m_high = br.readFloat();
            data.m_low = br.readFloat();
            data.m_upperLimit = br.readFloat();
            data.m_lowerLimit = br.readFloat();
            data.m_ratio = br.readFloat();
            data.m_pe = br.readFloat();
            data.m_turnoverRate = br.readFloat();
            data.m_marketValue = br.readFloat();
            data.m_flow = br.readFloat();
            data.m_speed = br.readFloat();
            priceDatas.add(data);
        }
    }
    return priceDatas;
}

void PriceDataServiceClient::onReceive(FCMessage *message) {
    FCClientService::onReceive(message);
    sendToListener(message);
}


